import 'package:distribution_mall_flutter/constant/http_constant.dart';
import 'package:distribution_mall_flutter/entity/address/address_entity.dart';
import 'package:distribution_mall_flutter/entity/address/address_query_entity.dart';
import 'package:distribution_mall_flutter/http/init.dart';
import 'package:distribution_mall_flutter/http/response_data.dart';
import 'package:distribution_mall_flutter/pages/address/address_search/domain/address_search_entity.dart';
import 'package:distribution_mall_flutter/pages/address/domain/action_address_item_entity.dart';
import 'package:flutter/foundation.dart';

class AddressApi {
  // 增加分销商地址
  static Future<Result<ActionAddressItemBody>> add(Map<String, dynamic> params) async {
    return await Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/distributor-address/add')
        .request<ActionAddressItemBody>(httpMethod: HttpMethod.post,params: params);
  }

  // 删除分销商地址
  static Future<Result<dynamic>> remove(String distributorAddressId) async {
    return Request(HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/distributor-address/remove')
        .request(
            params: {'distributorAddressId': distributorAddressId},
            httpMethod: HttpMethod.post,
            showLoadingIndicator: true);
  }

  // 更新分销商地址
  static Future<Result<ActionAddressItemBody>> update(Map<String, dynamic> params) async {
    return await Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/distributor-address/update')
        .request<ActionAddressItemBody>(httpMethod: HttpMethod.post,params: params);
  }

  // 分销商地址分页查询
  static Future<Result<AddressEntity>> pageQuery(int pageNum,int pageSize) async {
    return Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/distributor-address/pageQuery')
        .request<AddressEntity>(httpMethod: HttpMethod.post,params:  {'pageCondition': {
    'pageNum': pageNum,
    'pageSize': pageSize,
    }});
  }

  // 设置默认地址
  static Future updateIsDef(Map<String, dynamic> params) async {
    ResponseJsonBody response = await Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/distributor-address/updateIsDef')
        .post(params);
    return response.status!.success == true;
  }

  // 根据地址获取发货时效
  static Future<Result<List<AddressDeliveryTimeEntity>?>>  getAddressDeliveryTime(String deliveryAddressId) async {
    return Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/delivery/getAddressDeliveryTime')
        .request<List<AddressDeliveryTimeEntity>>(params: {'deliveryAddressId': deliveryAddressId},httpMethod: HttpMethod.post);
  }

  // 地址服务-查询下级地址
  static Future querySubAddresses(Map<String, dynamic> map) async {
    ResponseJsonBody response = await Request(
            HttpConstant.getDomain.userCenterApiGatewayUrl,
            '/common-open-biz/basic-data-center/address/querySubAddresses')
        .post(map);
    if (response.status?.success != true) return;
    return addressQueryEntityFromJson(response.body);
  }

  // 地址服务-搜索地址
  static Future<Result<List<AddressSearchBody>>> queryAddressByName(
      String name) {
    return Request(HttpConstant.getDomain.userCenterApiGatewayUrl,
            '/common-open-biz/basic-data-center/address/queryAddressByName')
        .request<List<AddressSearchBody>>(
            params: {'name': name, 'nationCode': 'PH'},
            httpMethod: HttpMethod.post);
  }
}
